/*
 * Copyright (c) 2022  by Junzhao
 * THIS FILE IS PART OF JAVA-Data-Structures-and-Algorithms PROJECT
 * All Rights Reserved.
 */

package Google上机题;

/**
 * @Description 使用散列表
 * google公司的一个上机题:
 * 有一个公司,当有新的员工来报道时
 * ,要求将该员工的信息加入(id,性别,年龄,住址..),当输入该员工的id时,
 * 要求查找到该员工的 所有信息.
 * <p>
 * 要求: 不使用数据库,尽量节省内存,速度越快越好 添加时，保证按照id从低到高插入
 * [并思考思考：如果id不是从低到高插入，但要求各条链表仍是从低到高]
 * @Author 俊昭
 * @Date 2022/5/2
 */
public class HashTable {
    private static final Emp[] EMPs = new Emp[10];// 雇员数组
//    private final int[] empCount = new int[10];// 记录每一个数组下有几个员工

    static {
        for (int i = 0; i < 101; i++) {
            String s = new String(new char[]{(char) (i % 26 + 97)});
            add(new Emp(i, s));
        }
    }

    // 增
    public static void add(Emp newEmp) {
        int x = newEmp.getId() % 10;
        if (EMPs[x] == null) {
            EMPs[x] = newEmp;
        } else {
            Emp temp = EMPs[x];
            while (temp.next != null) {
                temp = temp.next;
            }
            temp.next = newEmp;
        }
    }

    // 删
    public static void del(int ID) {
        if (isEmpty()) {
            System.out.println("还未存入雇员！");
            return;
        }
        int x = ID % 10;
        if (EMPs[x] != null) {
            if (EMPs[x].getId() == ID) {
                if (EMPs[x].next == null) {
                    EMPs[x] = null;
                } else {
                    EMPs[x] = EMPs[x].next;
                }
                System.out.println("删除成功");
                return;
            }
            Emp temp = EMPs[x];
            while (temp.next != null) {
                if (temp.next.getId() == ID) {
                    if (temp.next.next == null) {
                        temp.next = null;
                    } else {
                        temp.next = temp.next.next;
                    }
                    System.out.println("删除成功");
                    return;
                }
                temp = temp.next;
            }
            System.out.println("没找到改员工");
        } else {
            System.out.println("没找到改员工");
        }
    }

    // 改
    public static void set(Emp newEmp) {
        if (isEmpty()) {
            System.out.println("还未存入雇员！");
            return;
        }
        int ID = newEmp.getId();
        int x = ID % 10;
        if (EMPs[x] != null) {
            Emp temp = EMPs[x];
            while (temp.next != null) {
                if (temp.next.getId() == ID) {
                    if (temp.next.next != null) {
                        newEmp.next = temp.next.next;
                    }
                    temp.next = newEmp;
                    System.out.println("修改成功");
                    return;
                }
                temp = temp.next;
            }

        }
        System.out.println("没找到改员工");
    }

    // 查
    public static Emp get(int ID) {
        if (isEmpty()) {
            throw new RuntimeException("还未存入雇员！");
        }
        int x = ID % 10;
        if (EMPs[x] != null) {
            Emp temp = EMPs[x];
            while (temp.next != null) {
                if (temp.next.getId() == ID) {
                    return temp.next;
                }
                temp = temp.next;
            }
        }
        throw new RuntimeException("该员工不存在");

    }

    // 展示
    public static void show() {
        if (isEmpty()) {
            throw new RuntimeException("还未存入雇员！");
        }
        for (Emp emp : EMPs) {
            if (emp != null) {
                Emp temp = emp;
                while (temp.next != null) {
                    System.out.print(temp + "\t");
                    temp = temp.next;
                }
                System.out.print(temp + "\t");
                System.out.println();
            }
        }
    }

    // 判空
    private static boolean isEmpty() {
        boolean flag = true;
        for (Emp emp : EMPs) {
            if (emp != null) {
                flag = false;
                break;
            }
        }
        return flag;
    }

}
